<?php
//require_once  __DIR__ . '/../../includes/authenticate.php';
require_once  __DIR__ . '/includes/authenticate.php';
include_once __DIR__ . '/db_bb_connect.php';

require_once __DIR__ . '/includes/BasketbalStatsTableFunctions.php';

$basketbalfunctions = new BasketbalStatsTableFunctions();

?>

<style>

table {
	font-size: 14px;
	}

@media print
{    
    .hidden-print, .hidden-print *
    {
        display: none !important;
    }
}

#resultstable td a{
	text-decoration: none;
	color: black;
}

#resultstablea td:hover { 
   background: lightgrey; 
   cursor: pointer;
}

#resultstablea td a { 
   display: block; 
   width:100%;
}


</style>
<body>

<div id="container-fluid">
<div id="row">

<div id="content">	
<h2>Game Stats Archive</h2>

<form method="post" action="" class="hidden-print"> 
	<div class="form-group">
	  <div class="col-md-offset-3 col-md-6">        
	<select name="season" class="form-control">

<?php

if (isset($_POST['season'])) {
	$setseason = $_POST['season'];

	echo "<option id=\"selected-season\" value=\"$setseason\" selected=\"selected\">$setseason is selected</option>";
} else {
	echo "<option id=\"selected-season\" value=\"\" disabled=\"disabled\" selected=\"selected\">Select a season</option>";
}

?>

<?php

$username = $_SESSION['username'];

$sql = "SELECT type, user_key FROM users WHERE username = '$username'";

  if ($db->query($sql)) {  
    } else {
      echo "Error: " . $sql . "<br>" . $db->error;
    }

  $result = $db->query($sql);
  $row = $result->fetch_assoc();

  $type = $row['type'];
  $playerID = $row['user_key'];

if ($type == 'Player') {

	
	
	$sql = "SELECT DISTINCT season 
		FROM teams
		WHERE playerID = '$playerID'
		ORDER BY season";

} elseif ($type == 'Coach') {
	
	$sql = "SELECT DISTINCT season 
		FROM stats
		ORDER BY season";
}

$result = $db->query($sql);

while ($row = $result->fetch_assoc()) {
				$season = $row['season'];
				echo "<option value=\"$season\">$season</option>";
};

?>

	</select>
	</div>
	</div>

</form>

<?php

if (isset($_POST['season'])) {
	$setseason = $_POST['season'];

?>
</div>
</div>
</div>
<div id="container-fluid">
<div class="row">
<div id="statscontent" class="col-md-12">		
<div id="resultstable" class="table-responsive hidden-print">

<h2>Results</h2>
<table class="table table-hover table-striped table-condensed">
    <thead>
      <tr>
        <th>Date</th>
        <th colspan="2">Game</th>
        <th>Result</th>
        <th>Summary</th>
        <th>Stats</th>
      </tr>
    </thead>
    <tbody>
     
<?php

$sql = "SELECT * FROM games WHERE season = '$setseason' ORDER BY date ASC";
$result=$db->query($sql);

$sql2 = "SELECT DISTINCT teamname FROM teams WHERE season = '$setseason' LIMIT 1";
$result2=$db->query($sql2);
$row2=$result2->fetch_assoc();

$seasonteamname = $row2['teamname'];

while($row=$result->fetch_assoc()){	
	$gamenumber = $row['gamenumber'];
	$date = $row['date']; 
	$opponent = $row['opponent']; 
	$location = $row['location']; 
	$score_exc = $row['score_exc_tot']; 
	$score_opp = $row['score_opp_tot'];
	$season = $row['season']; 
	$gamenumber = $row['gamenumber']; 
	

	echo "<tr>";
    echo "<td><a href='#' gamenumber='$gamenumber' season='$season'> $date </a></td>";
          
	if ( $location == 'home' ) {

		echo "<td>  $seasonteamname  </td>";
		echo "<td>  $opponent </td>";
		echo "<td>  $score_exc - $score_opp </td>";

	} else { 
		echo "<td> $opponent </td>";
		echo "<td> $seasonteamname </td>"	;	
		echo "<td>$score_opp - $score_exc</td>";
	}

		if ($date <= date("Y-m-d") ) {
			echo "<td> 
				<form method='post' action='./game_summary.php' target='_blank'>

				<input type='hidden' name='season' value='$season'/>
				<input type='hidden' name='gamenumber' value='$gamenumber'/> 
				<input type='hidden' name='username' value='$username'/> 

				<button type='submit' class='btn btn-sm btn-default' name='summary' value='summary'>Summary</button>
				</form>
				</td>";


			} else {
				echo "<td></td>";
			}

echo "<td>";

	if ( $type == "Player") {

		if ( $basketbalfunctions->CheckGameStatsTotalsAvailable($season, $gamenumber) ) {

		echo "
			<form method='post' action='./game_stats_archive_tableloader.php' target='_blank'>

			<input type='hidden' name='season' value='$season'/>
			<input type='hidden' name='gamenumber' value='$gamenumber'/> 
			<input type='hidden' name='opponent' value='$opponent'/> 
			<input type='hidden' name='coachteam' value='$seasonteamname'/> 
			<input type='hidden' name='date' value='$date'/> 
			<input type='hidden' name='location' value='$location'/>
			<input type='hidden' name='type' value='$type'/>
			<input type='hidden' name='score_opp' value='$score_opp'/>
			<input type='hidden' name='score_coachteam' value='$score_exc'/>

			<button type='submit' class='btn btn-sm btn-success' name='quarter' value='fullgameold'>Stats</button>

			</form>";

	} elseif ( $basketbalfunctions->CheckGameStatsAllQuartersAvailable($season, $gamenumber, $seasonteamname) ) {

			echo "
			<form method='post' action='./game_stats_archive_tableloader.php' target='_blank'>

			<input type='hidden' name='season' value='$season'/>
			<input type='hidden' name='gamenumber' value='$gamenumber'/> 
			<input type='hidden' name='opponent' value='$opponent'/> 
			<input type='hidden' name='coachteam' value='$seasonteamname'/> 
			<input type='hidden' name='date' value='$date'/> 
			<input type='hidden' name='location' value='$location'/>
			<input type='hidden' name='type' value='$type'/>

						<input type='hidden' name='score_opp' value='$score_opp'/>
			<input type='hidden' name='score_coachteam' value='$score_exc'/>


			<button type='submit' class='btn btn-sm btn-success' name='quarter' value='fullgame'>Stats</button>

			</form>";

	} else {
		echo "No Stats Available";
	}
		
	} elseif ( $type = "Coach" ) {

	if ( $basketbalfunctions->CheckGameStatsQuarterAvailable($season, $gamenumber) ){

		echo "
			<form method='post' action='./game_stats_archive_tableloader.php' target='_blank'>

				<input type='hidden' name='season' value='$season'/>
				<input type='hidden' name='gamenumber' value='$gamenumber'/> 
				<input type='hidden' name='opponent' value='$opponent'/> 
				<input type='hidden' name='coachteam' value='$seasonteamname'/> 
				<input type='hidden' name='date' value='$date'/> 
				<input type='hidden' name='location' value='$location'/>
				<input type='hidden' name='type' value='$type'/>

				<input type='hidden' name='score_opp' value='$score_opp'/>
				<input type='hidden' name='score_coachteam' value='$score_exc'/>

			<div class='btn-group'>
				<button type='submit' class='btn btn-sm btn-success' name='quarter' value='fullgame'>Full Game</button>
				<button type='submit' class='btn btn-sm btn-success' name='quarter' value='fullgameold'>Full Game Old</button>
				<button type='submit' class='btn btn-sm btn-success' name='quarter' value='firsthalf'>1st</button>
				<button type='submit' class='btn btn-sm btn-success' name='quarter' value='secondhalf'>2nd</button>
				<button type='submit' class='btn btn-sm btn-success' name='quarter' value='1'>Q1</button>
				<button type='submit' class='btn btn-sm btn-success' name='quarter' value='2'>Q2</button>
				<button type='submit' class='btn btn-sm btn-success' name='quarter' value='3'>Q3</button>
				<button type='submit' class='btn btn-sm btn-success' name='quarter' value='4'>Q4</button>";

				if ($basketbalfunctions->CheckGameStatsOTAvailable($season, $gamenumber)){
					
					echo "<button type='submit' class='btn btn-sm btn-success' name='quarter' value='OT'>OT</button>"; 
				}

				echo "
				</div>
			</form>
			";

	} elseif ( $basketbalfunctions->CheckGameStatsTotalsAvailable($season, $gamenumber) ) {

		echo " 

			<form method='post' action='./game_stats_archive_tableloader.php' target='_blank'>

			<input type='hidden' name='season' value='$season'/>
			<input type='hidden' name='gamenumber' value='$gamenumber'/> 
			<input type='hidden' name='opponent' value='$opponent'/> 
			<input type='hidden' name='coachteam' value='$seasonteamname'/> 
			<input type='hidden' name='date' value='$date'/> 
			<input type='hidden' name='location' value='$location'/>
			<input type='hidden' name='type' value='$type'/>

			<input type='hidden' name='score_opp' value='$score_opp'/>
			<input type='hidden' name='score_coachteam' value='$score_exc'/>

			<button type='submit' class='btn btn-sm btn-success' name='quarter' value='fullgameold'>Full Game</button>
			</form>
			";

	} else {
		echo "No Stats Available";
	}



}
	echo "</td>";
	echo "</tr>";
}

?>
    </tbody>
  </table>

<div id='seasonstats'>

<strong>Games Won: </strong> 
<?php

$sql = "SELECT COUNT(*) AS GamesWon FROM games WHERE season = '$setseason' AND score_exc_tot > score_opp_tot";

$result=$db->query($sql);
$row=$result->fetch_assoc();

$gameswon = $row['GamesWon'];

echo $gameswon;

?>
<strong> Games Lost: </strong>
<?php

$sql = "SELECT COUNT(*) AS GamesLost FROM games WHERE season = '$setseason' AND score_exc_tot < score_opp_tot";

$result=$db->query($sql);
$row=$result->fetch_assoc();

$gameswon = $row['GamesLost'];

echo $gameswon;
?>
<strong> Total Score: </strong>

<?php

$sql = "SELECT SUM(score_exc_tot) AS SumExc, SUM(score_opp_tot) AS SumOpp  FROM games WHERE season = '$setseason'";

$result=$db->query($sql);
$row=$result->fetch_assoc();

$SumExc = $row['SumExc'];
$SumOpp = $row['SumOpp'];

echo "$SumExc - $SumOpp";
?>
<br><br>
<form>
<input type="hidden" name="season" value="<?= $setseason; ?>">
    <button type="submit" class="btn btn-success showcumustatsbutton submit">Cumulative Stats</button>
    </form>
</br>

</div> 
	</div>
</div>
</div>
</div>
</div>

<?php

}

?>
</div>

<script>

 $(".showcumustatsbutton").on("click", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
	  type: "POST",
	  url: "./game_stats_archive_cumustatstable.php",
	  data: postData,
	  success: function(data) {
	  	var w = window.open();
	  	$(w.document.body).html(data);
  	  }
  }); 

  e.preventDefault();
});

$("select").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
	  type: "POST",
	  url: "./game_stats_archive.php",
	  data: postData,
	  success: function(data) {
	  	$("#content").html(data);
  	  }
  }); 
  e.preventDefault();

});

</script>
</body>
</html>